package com.etao.tools.common;

import com.etao.tools.entity.TurnOverListByDayEntity;
import com.etao.tools.entity.userDishInfoEntity;
import com.etao.tools.entity.userInfoEntity;
import org.apache.poi.hssf.usermodel.*;
import org.slf4j.LoggerFactory;
import java.io.FileOutputStream;
import java.util.List;

/**
 * Created by lyp on 2016/8/8.
 */
public class ExportExcel {
    protected static final org.slf4j.Logger log = LoggerFactory.getLogger("com.etao.restaurantapi");
    private static HSSFWorkbook wb = null;

    public static ExportExcel getInstance() {
        wb = new HSSFWorkbook();
        return new ExportExcel();
    }

    /*
    创建Sheet
     */
    private static HSSFSheet CreateSheet(String sheetName,String[] list)
    {
        HSSFSheet sheet = wb.createSheet(sheetName);
        HSSFRow row = sheet.createRow((int) 0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(font);

        HSSFCell cell=null;
       for(int i=0;i<list.length;i++)
       {
           cell = row.createCell(i);
           cell.setCellValue(list[i]);
           cell.setCellStyle(style);
       }

        return sheet;
    }

    /*
日新用户表
     */
    public static void AddNewUserlistSheet(List<userInfoEntity> data) {
        if (data != null) {
            String[] list=new String[]{"客户ID","OpenId","昵称"}; //列数
            HSSFSheet sheet=CreateSheet("日新用户表",list);
            HSSFRow row =null;

            for (int i = 0; i < data.size(); i++) {
                row = sheet.createRow((int) i + 1);
                userInfoEntity stu = (userInfoEntity) data.get(i);
                row.createCell(0).setCellValue(stu.getCustomerId()); //第一列
                row.createCell(1).setCellValue(stu.getOpenId()); //第二列
                row.createCell(2).setCellValue(stu.getNickName()); //第三列
            }
        }
    }

    /*
    经常到店消费老客户表
     */
    public static void AddUserListSheet(List<userInfoEntity> data) {
        if (data != null) {
            String[] list=new String[]{"客户ID","OpenId","昵称"}; //列数
            HSSFSheet sheet=CreateSheet("经常到店消费老客户表",list);
            HSSFRow row =null;

            for (int i = 0; i < data.size(); i++) {
                row = sheet.createRow((int) i + 1);
                userInfoEntity stu = (userInfoEntity) data.get(i);
                row.createCell(0).setCellValue(stu.getCustomerId()); //第一列
                row.createCell(1).setCellValue(stu.getOpenId()); //第二列
                row.createCell(2).setCellValue(stu.getNickName()); //第三列
            }
        }
    }

    /*
    7日未复购客户表
     */
    public static void AddReUserListSheet(List<userInfoEntity> data) {
        if (data != null) {
            String[] list=new String[]{"客户ID","OpenId","昵称"}; //列数
            HSSFSheet sheet=CreateSheet("7日未复购客户表",list);
            HSSFRow row =null;

            for (int i = 0; i < data.size(); i++) {
                row = sheet.createRow((int) i + 1);
                userInfoEntity stu = (userInfoEntity) data.get(i);
                row.createCell(0).setCellValue(stu.getCustomerId()); //第一列
                row.createCell(1).setCellValue(stu.getOpenId()); //第二列
                row.createCell(2).setCellValue(stu.getNickName()); //第三列
            }
        }
    }

    /*
    店内日消费表
     */
    public static void AddCostListSheet(List<userDishInfoEntity> data) {
        if (data != null) {
            String[] list=new String[]{"客户ID","OpenId","昵称","购买菜品"}; //列数
            HSSFSheet sheet=CreateSheet("店内日消费表",list);
            HSSFRow row =null;

            for (int i = 0; i < data.size(); i++) {
                row = sheet.createRow((int) i + 1);
                userDishInfoEntity stu = (userDishInfoEntity) data.get(i);
                row.createCell(0).setCellValue(stu.getCustomerId()); //第一列
                row.createCell(1).setCellValue(stu.getOpenId()); //第二列
                row.createCell(2).setCellValue(stu.getNickName()); //第三列
                row.createCell(3).setCellValue(stu.getDishs()); //第三列
            }
        }
    }

    /*
    店内每日营业额
     */
    public static void AddTurnOverListByDaySheet(List<TurnOverListByDayEntity> data)
    {
        if (data != null) {
            String[] list=new String[]{"门店ID","门店名称","日营业额"}; //列数
            HSSFSheet sheet=CreateSheet("店内日消费表",list);
            HSSFRow row =null;

            for (int i = 0; i < data.size(); i++) {
                row = sheet.createRow((int) i + 1);
                TurnOverListByDayEntity stu = (TurnOverListByDayEntity) data.get(i);
                row.createCell(0).setCellValue(stu.getSupplierID()); //第一列
                row.createCell(1).setCellValue(stu.getSupplierName()); //第二列
                row.createCell(2).setCellValue(stu.getCustomerTotal()); //第三列
            }
        }
    }

    public static void Export(String xlsName) {
        try {
            String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().replace("%20", " ") + xlsName;
            System.out.println(path);
            FileOutputStream fout = new FileOutputStream(path);
            wb.write(fout);
            fout.close();
            wb = null;
            log.debug("导出报表成功。导出路径为：" + path);
        } catch (Exception e) {
            log.debug("导出报表失败，原因：" + e.getMessage());
            e.printStackTrace();
        }
    }

}
